<?php
namespace app\controllers;

use yii;
use yii\helpers\Url;

class BaseController extends yii\web\Controller {

	public function actions() {
		return [
			'captcha'=>[
				'class'=>'yii\captcha\CaptchaAction',
				'fixedVerifyCode'=>YII_ENV_TEST ? 'testme' : null,
				'minLength'=>4,
				'maxLength'=>4
			]
		];
	}

	public function beforeAction($action) {
		if ($this->isAjax()) {
			Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
		}
		if (!in_array($this->route, [
			'site/test',
			'site/index',
			'site/login',
			'site/logout',
			'site/about',
			'site/error'
		]) && Yii::$app->user->isGuest) {
			$this->redirect([
				'/site/login'
			]);
			return false;
		}
		return true;
	}

	public function isAjax() {
		return Yii::$app->request->isAjax || isset($_REQUEST['ajax']);
	}
}